﻿using System;
using System.Collections.Generic;
using SortOf;

namespace ConsoleApplication1
{
    class Program
    {
        private Random random;
        private List<IComparable> list;

        public Program()
        {
            random = new Random();
            list = new List<IComparable>();

            // Fill the array with random numbers.
            for (int i = 0; i < 100; i++)
            {
                list.Add(random.Next(0, int.MaxValue));
            }

            IComparable match = list[5];

            Console.WriteLine("The program will search for the value " + list[5].ToString());
            Console.WriteLine();

            // Copy the contents of the list into a temporary array.
            IComparable[] temp1 = new IComparable[list.Count];
            list.CopyTo(temp1);

            // Search for the first occurence of 0 and retrieve its index in the list.
            Console.WriteLine("This is prone to failure, because the list has to be sorted in ascending order for the search algorithm to work properly. \r\n\tIt returned: " + BinarySearch.RbinSearch(temp1, match, 0, temp1.Length - 1));
            Console.WriteLine();

            // Sort the list and show the result.
            list.Sort();
            IComparable[] temp2 = new IComparable[list.Count];
            list.CopyTo(temp2);

            // Search for the first occurence of 0 and retrieve its index in the list.
            Console.WriteLine("Now that the list is sorted it is will get the proper index. \r\n\tIt returned position: " + BinarySearch.RbinSearch(temp2, match, 0, temp2.Length - 1));
            Console.WriteLine();

            // Additional information regarding the code.
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine(i + " | " + list[i]);
            }
        }

        static void Main(string[] args)
        {
            new Program();

            // Hold till the ENTER key has been pressed.
            ConsoleKeyInfo info = Console.ReadKey();
            while (info.Key != ConsoleKey.Enter)
                info = Console.ReadKey();
        }
    }
}
